import ClientManager from "../../../Framework/Scripts/Managers/ClientManager";
import ConfirmManager from "../../../Framework/Scripts/Managers/ConfirmManager";
import NativeManager from "../../../Framework/Scripts/Managers/NativeManager";
import NetManager from "../../../Framework/Scripts/Net/NetManager";
import NetWork from "../../../Framework/Scripts/Net/NetWork";
import { Form, ShareDefine } from "../Config/ShareDefine";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ControlManager extends cc.Component {
    public static Instance: ControlManager = null;
    isAndroid: any;
    onLoad() {
        if (ControlManager.Instance !== null) {
            this.destroy();
            return;
        }
        ControlManager.Instance = this;
    }

    IsEnableCheckVpn () {
        let clientConfig = ClientManager.Instance.GetClientConfig();
        if (!clientConfig) {
            return false;
        }

        let value = clientConfig["isEnableCheckVpn"];
        if ("false" == value) return false;
        if ("true" == value) return true;
        
        return false;
    }

    CheckVpnUsed(clickCb = null) {
        if (this.IsOpenVpn()) {
            NetManager.Instance.Disconnect();
            this.SetWaitForConfirm('DNS中断', Form.ConfirmOK, [], ["WEBSOCKET_DISCONNECT_VPN_CHECK"], "", clickCb);
            return true;
        }
        return false;
    }

    
    SetWaitForConfirm (msgID, type, msgArg = [], cbArg = [], content = "", clickCb) {
        if (!!clickCb) {
            console.log("clickCb", clickCb)
            ConfirmManager.Instance.SetWaitForConfirmForm(() => {
                console.log("VPN 断连后，需要重新建立连接 clickCb");
                this.CheckVpnUsed();
                clickCb();
            }, msgID, cbArg);
        } else {
            ConfirmManager.Instance.SetWaitForConfirmForm(this.OnConFirm.bind(this), msgID, cbArg);
        }
        ConfirmManager.Instance.ShowConfirm(type, msgID, msgArg, content);
    }

    
    OnConFirm (clickType, msgID, backArgList) {
        if (clickType != "WEBSOCKET_DISCONNECT_VPN_CHECK") {
            console.log("VPN 断连后，需要重新建立连接 OnConFirm");
            NetWork.Instance.ReConnect();
            this.CheckVpnUsed();
        }
    }

    //---------------加载资源-----------------

    IsOpenVpn () {
        return false;
        
        if (!this.isAndroid) {
            return false;
        }

        if (!this.IsEnableCheckVpn()) {
            console.log("未启用VPN检测");
            return false;
        }

        let isOpenVpn = NativeManager.Instance.CallToNative("CheckVpnUsed", [], "Boolean");
        if (isOpenVpn) {
            console.log("检测到VPN为开启状态");
        }
        return isOpenVpn;
    }
}
